<?php
class Server {
  public static function submit_page($orm) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    if ($req->name == '') {
      echo 'Page is empty';
      return;
    }
    
    if (isset($req->__noico) && $req->__noico == 1 && file_exists($req->icon)) {
      unlink($req->icon);
      $req->icon = '';
      unset($req->__noico);
    }
    
    if (isset($req->is_default) && $req->is_default == 1) {
      $orm->exec('UPDATE pb_page SET is_default = 0 WHERE sys_id = ' . $req->sys_id);
    }
    
    if (isset($req->id)) {
      $orm->update('pb_page', $req);
    } else {
      $opts = new stdClass();
      $opts->cd = 't.sys_id = :v1';
      $opts->cdv = array(':v1' => $req->sys_id);
      $opts->sel = 't.order';
      $pages = $orm->get('pb_page', 't.order', 'DESC', 1, 1, $opts)->d;
      if (count($pages) > 0) {
        $req->order = $pages[0]->order + 1;
      }
      
      $orm->add('pb_page', $req);
      $req->id = $orm->last_id();
    }
    
    include '__clear.php';
    echo json_encode($req);
  }
  
  public static function submit_instance($orm) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    if ($req->name == '') {
      echo 'Name is empty';
      return;
    }
    
    $opts = new stdClass();
    $opts->cd = 't.id = :v1';
    $opts->cdv = array(':v1' => $req->page_id);
    $opts->sel = 't.id, t.html';
    $pages = $orm->get('pb_page', NULL, NULL, NULL, NULL, $opts)->d;
    if (count($pages) > 0) {
      if (isset($req->id)) {
        $opts = new stdClass();
        $opts->cd = 't.id = :v1';
        $opts->cdv = array(':v1' => $req->id);
        $opts->sel = 't.*, t1.folder AS __folder';
        $opts->jt = 'LEFT JOIN pb_plugin AS t1 ON t1.id = t.plugin_id';
        $instances = $orm->get('pb_plugin_instance', NULL, NULL, NULL, NULL, $opts)->d;
        if (count($instances) > 0) {
          $pages[0]->html = str_replace(Utils::get_inst_cd($instances[0]), Utils::get_inst_cd($req), $pages[0]->html);
          $orm->update('pb_page', $pages[0]);
        }
        
        $orm->update('pb_plugin_instance', $req);
      } else {
        $orm->add('pb_plugin_instance', $req);
        $req->id = $orm->last_id();
        $pages[0]->html = '<p>' . Utils::get_inst_cd($req) . '</p>' . $pages[0]->html;
        $orm->update('pb_page', $pages[0]);
      }
    }
    
    include '__clear.php';
    echo json_encode($req);
  }
  
  public static function delete_instance($orm) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $opts = new stdClass();
    $opts->cd = 't.id = :v1';
    $opts->cdv = array(':v1' => $req->page_id);
    $opts->sel = 't.id, t.html';
    $pages = $orm->get('pb_page', NULL, NULL, NULL, NULL, $opts)->d;
    if (count($pages) > 0) {
      $pages[0]->html = str_replace('<p>' . Utils::get_inst_cd($req) . '</p>', '', $pages[0]->html);
      $pages[0]->html = str_replace(Utils::get_inst_cd($req), '', $pages[0]->html);
      $orm->update('pb_page', $pages[0]);
    }
    
    $orm->delete('pb_plugin_instance', $req);
    
    include '__clear.php';
    echo json_encode($req);
  }
  
  public static function change_page($orm) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $rslt = new stdClass();
    $rslt->page = NULL;
    $rslt->instances = array();
    $opts = new stdClass();
    $opts->cd = 't.id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    $pages = $orm->get('pb_page', NULL, NULL, NULL, NULL, $opts)->d;
    if (count($pages) > 0) {
      $rslt->page = $pages[0];
    }
    
    $opts = new stdClass();
    $opts->cd = 't.page_id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    $opts->sel = 't.*, t1.folder AS __folder';
    $opts->jt = 'LEFT JOIN pb_plugin AS t1 ON t1.id = t.plugin_id';
    $rslt->instances = $orm->get('pb_plugin_instance', 't.name', 'ASC', NULL, NULL, $opts)->d;
    echo json_encode($rslt);
  }
  
  public static function upload_icon($orm) {
    if (isset($_FILES['Filedata']) && is_uploaded_file($_FILES['Filedata']['tmp_name']) && $_FILES['Filedata']['error'] == 0) {
      //获得上传文件信息
      $upload_file = $_FILES['Filedata'];
      //获取文件扩展名
      $file_info = pathinfo($upload_file['name']);
      $file_ext = (isset($file_info['extension'])) ? $file_info['extension'] : '';
      
      //获取客户端参数
      $id = Utils::get_param('id', 0);
      
      $opts = new stdClass();
      $opts->cd = 't.id = :v1';
      $opts->cdv = array(':v1' => $id);
      $opts->sel = 't.id, t.icon';
      $rslt = $orm->get('pb_page', NULL, NULL, NULL, NULL, $opts)->d;
      if (count($rslt) > 0) {
        //Add custom code here...
        $rslt[0]->__icon = iconv('UTF-8', 'GB2312', $rslt[0]->icon);
        if (file_exists($rslt[0]->__icon)) {
          unlink($rslt[0]->__icon);
        }
        $file_name = $_FILES['Filedata']['name'];
        $file_ext = strtolower((isset($file_info['extension'])) ? $file_info['extension'] : '');
        $file_name = str_replace('.' . $file_ext, '', $file_name);
        $file_name = iconv('UTF-8', 'GB2312', $file_name);
        $rslt[0]->icon = UPLOAD_DIR_INSTL . $file_name . '_' . Utils::unique() . '.' . $file_ext;
        if (move_uploaded_file($upload_file['tmp_name'], $rslt[0]->icon)) {
          $rslt[0]->icon = iconv('GB2312', 'UTF-8', $rslt[0]->icon);
          $orm->update('pb_page', $rslt[0]);
          echo json_encode($rslt[0]);
        }
      }
    }
  }
}
?>